//
//  PageScrollFooterView.m
//  ebook
//
//  Created by mt on 16/8/19.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "PageScrollFooterView.h"
#import "ReaderGlobalModel.h"
#import "ALBatteryView.h"
#import "PCReaderTool.h"

@interface PageScrollFooterView ()

@property (nonatomic, strong) PCReaderTool *readerTool;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) ALBatteryView *batteryView;

@end

@implementation PageScrollFooterView

- (instancetype)init {
    
    self = [super initWithFrame:CGRectMake(10, MTScreenH-25, MTScreenW-20, 25)];
    if (self = [super init]) {
        [self addSubview:self.batteryView];
        [self addSubview:self.timeLabel];
        [self addSubview:self.progressLabel];
        [self startMonitorTime];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBgColor:) name:kChangePageColorNotification object:nil];
    
    return self;
}

-(void)updateBgColor:(NSNotification *)noti{
    
    UIColor *chapterColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
    self.timeLabel.textColor = chapterColor;
    self.progressLabel.textColor = chapterColor;
    self.batteryView.batteryFill.backgroundColor = chapterColor;
}

- (ALBatteryView *)batteryView {
    
    if (!_batteryView) {
        _batteryView = [[ALBatteryView alloc] initWithFrame:CGRectMake(self.width-20-10, 2.5, 20, 20)];
    }
    [_batteryView setBatteryLevelWithColor:[ReaderGlobalModel shareModel].readerConfig.TitleTextColor forValue:[UIDevice currentDevice].batteryLevelInPercentage image:[ReaderGlobalModel shareModel].readerConfig.batteryImage];
    _batteryView.batteryFill.backgroundColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
    return _batteryView;
}

- (UILabel *)timeLabel {
    
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.frame = CGRectMake(10, 0, 50, 25);
        _timeLabel.textColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _timeLabel;
}

- (UILabel *)progressLabel {
    
    if (!_progressLabel) {
        _progressLabel = [[UILabel alloc] init];
        _progressLabel.frame = CGRectMake((MTScreenW-60)/2, 0, 60, 25);
        _progressLabel.textColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
        _progressLabel.font = [UIFont systemFontOfSize:12];
        _progressLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _progressLabel;
}

- (PCReaderTool *)readerTool {
    
    if (!_readerTool) {
        _readerTool = [[PCReaderTool alloc] init];
    }
    return _readerTool;
}

- (void)resetColor {
    
    UIColor *chapterColor = [ReaderGlobalModel shareModel].readerConfig.TitleTextColor;
    self.backgroundColor = chapterColor;
    self.timeLabel.textColor = chapterColor;
    self.progressLabel.textColor = chapterColor;
    self.batteryView.batteryFill.backgroundColor = chapterColor;
}

- (void)startMonitorTime {
    
    [self.readerTool startMonitorTimeWithBlock:^(NSDate *currentDate) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *datestr = [dateFormatter stringFromDate:currentDate];
        _timeLabel.text = datestr;
    }];
}

- (void)stopMonitorTime {
    
    [self.readerTool stopMonitorTime];
}

- (void)updateProgressPercent:(float)percent {

    if (percent<=0)
        percent = 0;
    if (percent>1)
        percent = 1;
    self.progressLabel.text = [NSString stringWithFormat:@"%.1f%%", percent*100];
}

-(void)dealloc{
    [self stopMonitorTime];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
